//
//  WALoginViewModel.m
//  WalkiOS
//
//  Created by luqiuan on 2019/10/22.
//  Copyright © 2019 Chenmuzhou. All rights reserved.
//

#import "WALoginViewModel.h"
@implementation WALoginModel

@end

@interface WALoginViewModel ()

//! <#PS:#>
@property (nonatomic, strong, readwrite) RACSignal *enableBtnSignal;
//! <#PS:#>
@property (nonatomic, strong, readwrite) RACCommand *loginCommond;
@end

@implementation WALoginViewModel

- (WALoginModel *)loginModel{
    if (!_loginModel) {
        _loginModel = [WALoginModel new];
    }
    return _loginModel;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self bindSignal];
    }
    return self;
}

- (void)bindSignal{
    _enableBtnSignal = [RACSignal combineLatest:@[RACObserve(self.loginModel, pwd),RACObserve(self.loginModel, account)] reduce:^id (NSString *account,NSString *pwd){
        return @(account.length && pwd.length);
    }];
    
    _loginCommond = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
        return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            [WNBusinessRequest loginWithPhone:self.loginModel.account pass:self.loginModel.pwd Succ:^(id  _Nonnull responseObject) {
                [subscriber sendNext:responseObject];
                [subscriber sendCompleted];
            } fail:^(NSString * _Nonnull code, NSString * _Nonnull mes) {
                [WNTool show:mes];
                [subscriber sendError:nil];
            }];
            return nil;
        }];
    }];
    [_loginCommond.executionSignals.switchToLatest subscribeNext:^(id  _Nullable x) {
    }];
    
    [_loginCommond.executing subscribeNext:^(NSNumber * _Nullable x) {
    }];
}

@end
